<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="任务人">
              <j-input placeholder="请选择任务人" v-model="queryParam.createBy"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="第几周">
              <a-input placeholder="请输入第几周" v-model="queryParam.weekProject"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="开始时间">
                <j-date placeholder="请选择开始时间" v-model="queryParam.startTime"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="结束时间">
                <j-date placeholder="请选择结束时间" v-model="queryParam.endTime"></j-date>
              </a-form-item>
            </a-col>

          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('双周计划表')">导出</a-button>-->
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        filterMultiple="filterMultiple"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
        :customRow="clickThenCheck"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定发布该计划吗?发布后该计划的任务不可编辑！" @confirm="() => releasePlan(record)">
                  <a>发布</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="对上周计划进行归档？" @confirm="() => summaryPlan(record)">
                  <a>归档</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <!-- table区域-end -->

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="任务表" key="1" >
        <HfBussinessProjectTasksList :mainId="selectedMainId" :planType="planType"/>
      </a-tab-pane>
    </a-tabs>

    <hf-bussiness-double-week-plan-modal ref="modalForm" @ok="modalFormOk"/>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import HfBussinessDoubleWeekPlanModal from './modules/HfBussinessDoubleWeekPlanModal'
  import { getAction } from '@/api/manage'
  import HfBussinessProjectTasksList from './HfBussinessProjectTasksList'
  import '@/assets/less/TableExpand.less'

  export default {
    name: "HfBussinessDoubleWeekPlanList",
    mixins:[JeecgListMixin],
    components: {
      HfBussinessProjectTasksList,
      HfBussinessDoubleWeekPlanModal
    },
    data () {
      return {
        description: '双周计划表管理页面',
        /* 分页参数 */
        ipagination:{
          current: 1,
          pageSize: 5,
          pageSizeOptions: ['5', '10', '20'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'任务人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'第几周',
            align:"center",
            dataIndex: 'weekProject'
          },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'startTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'endTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'任务完成率',
            align:"center",
            dataIndex: 'taskCompletion'
          },

          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'计划状态',
            align:"center",
            dataIndex: 'planType_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/bussiness/hfBussinessDoubleWeekPlan/list",
          delete: "/bussiness/hfBussinessDoubleWeekPlan/delete",
          deleteBatch: "/bussiness/hfBussinessDoubleWeekPlan/deleteBatch",
          exportXlsUrl: "/bussiness/hfBussinessDoubleWeekPlan/exportXls",
          importExcelUrl: "bussiness/hfBussinessDoubleWeekPlan/importExcel",
          changePlanType: "bussiness/hfBussinessDoubleWeekPlan/changePlanType",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      handleEdit: function (record) {
        if(record.planType != "1"){
          this.$message.error('请选择未发布状态的计划进行编辑!');
          return;
        }
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      },
      initDictConfig(){
        initDictOptions('').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'subjectId', res.result)
          }
        })
        initDictOptions('').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'planType', res.result)
          }
        })
      },
      releasePlan(record){
        if('1' != record.planType){
          this.$message.error('请操作计划状态为“未发布”的双周计划!');
          return;
        }
        getAction(this.url.changePlanType, record).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success("操作完成。")
          } else {
            this.$message.warning("操作失败！")
          }
        })
      },
      summaryPlan(record){
        if('3' != record.planType){
          this.$message.error('请操作计划状态为“已发布”的双周计划!');
          return;
        }
        getAction(this.url.changePlanType, record).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success("操作完成。")
          } else {
            this.$message.warning("操作失败！")
          }
        })
      },
      clickThenCheck(record) {
        return {
          on: {
            click: () => {
              this.onSelectChange(record.id.split(","), [record]);
            }
          }
        }
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectionRows = [];
        this.selectedMainId='';
        this.planType='';
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedMainId=selectedRowKeys[0];
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
        this.planType = selectionRows[0].planType;
      },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        this.onClearSelected()
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      getSuperFieldList(){
        let fieldList=[];
         fieldList.push({type:'string',value:'deleteFlag',text:'删除标记',dictCode:''})
        fieldList.push({ type: 'string', value: 'projectId', text: '项目id', dictCode: '' })
        fieldList.push({ type: 'string', value: 'projectShortName', text: '项目简称', dictCode: '' })
        fieldList.push({ type: 'string', value: 'weekPeoject', text: '第几周', dictCode: '' })
         fieldList.push({type:'string',value:'subjectId',text:'专题名称',dictCode:''})
         fieldList.push({type:'string',value:'targetId',text:'目标名称',dictCode:''})
         fieldList.push({type:'date',value:'startTime',text:'开始时间'})
         fieldList.push({type:'date',value:'endTime',text:'结束时间'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
